<template>
  <div class="dashboard-container" v-loading='loading'>
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-col>
        <span>郑州升达经贸管理学院</span>
      </el-col>
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- <tree-tools :tree-node="company" is-Root="true" /> -->
        <tree-tools :tree-node="company" is-Root @add-depts="OnAddDepts" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @add-depts="OnAddDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :showDialog.sync="showDialog"
      :tree-node="node"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import AddDept from "./components/add-dept.vue";
import treeTools from "./components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils/index";
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      loading:false,
      node: null,
      showDialog: false,
      company: {},
      departs: [],
      defaultProps: {
        label: "name", // 表示 从这个属性显示内容
      },
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading=true
      const res = await getDepartments();
      // console.log(res);
      this.company = { name: res.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeData(res.depts, "");
      this.$emit("OnAddDepts");
      this.loading=false

    },
    OnAddDepts(node) {
      this.showDialog = true; // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node;
    },
   

    async editDepts(node) {
      this.showDialog = true; // 显示新增组件弹层
      this.node = node; // 存储传递过来的node数据
      // 父组件 调用子组件的方法
      console.log(this.$refs.addDept);
      this.$refs.addDept.getDepartDetail(node.id); // 直接调用子组件中的方法 传入一个id
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
